<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="机构" prop="orgId">
        <el-input
          v-model="queryParams.orgId"
          placeholder="请输入机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文档名称" prop="documentName">
        <el-input
          v-model="queryParams.documentName"
          placeholder="请输入文档名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件类型" prop="documentType">
        <el-select v-model="queryParams.documentType" placeholder="请选择文件类型" clearable>
          <el-option
            v-for="dict in dict.type.submit_document_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="文件发布日期" prop="publishDate">
        <el-date-picker clearable
          v-model="queryParams.publishDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择文件发布日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="文件生效日期" prop="effectiveDate">
        <el-date-picker clearable
          v-model="queryParams.effectiveDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择文件生效日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="文件终止日期" prop="expiryDate">
        <el-date-picker clearable
          v-model="queryParams.expiryDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择文件终止日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['submit:document:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['submit:document:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['submit:document:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['submit:document:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="documentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="文档ID" align="center" prop="documentId" /> -->
      <el-table-column label="文档名称" align="center" prop="documentName" />
      <el-table-column label="所属机构" align="center" prop="orgId" />
      <el-table-column label="文件类型" align="center" prop="documentType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.submit_document_type" :value="scope.row.documentType"/>
        </template>
      </el-table-column>
      <el-table-column label="文件发布日期" align="center" prop="publishDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件生效日期" align="center" prop="effectiveDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectiveDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件终止日期" align="center" prop="expiryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['submit:document:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['submit:document:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报送文档对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="文档名称" prop="documentName">
          <el-input v-model="form.documentName" placeholder="请输入文档名称" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="机构" prop="orgId">
              <el-select v-model="form.orgId" placeholder="请选择机构">
                <el-option
                  v-for="item in this.orgList"
                  :key="item.orgId"
                  :label="item.orgName"
                  :value="item.orgId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文件类型" prop="documentType">
              <el-select v-model="form.documentType" placeholder="请选择文件类型">
                <el-option
                  v-for="dict in dict.type.submit_document_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="接收方信息" prop="receiverInfo">
          <el-input 
          v-model="form.receiverInfo" 
          placeholder="请输入接收方信息"
          type="textarea"
          :rows="3" 
          />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="发布日期" prop="publishDate">
              <el-date-picker clearable
                v-model="form.publishDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择文件发布日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生效日期" prop="effectiveDate">
              <el-date-picker clearable
                v-model="form.effectiveDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择文件生效日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="终止日期" prop="expiryDate">
          <el-date-picker clearable
            v-model="form.expiryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择文件终止日期">
          </el-date-picker>
        </el-form-item>
        <el-row>
          <el-col :span="12">
          </el-col>
          <el-col :span="12">
            <el-form-item label="请上传文件" prop="fileStorageId">
              <file-upload v-model="form.fileStorageId"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDocument, getDocument, delDocument, addDocument, updateDocument } from "@/api/submit/document";
import { listOrg} from "@/api/submit/org";

export default {
  name: "Document",
  dicts: ['submit_document_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报送文档表格数据
      documentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgId: null,
        documentName: null,
        documentType: null,
        publishDate: null,
        effectiveDate: null,
        expiryDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orgId: [
          { required: true, message: "机构不能为空", trigger: "blur" }
        ],
        documentName: [
          { required: true, message: "文档名称不能为空", trigger: "blur" }
        ],
        receiverInfo: [
          { required: true, message: "接收方信息不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建者ID不能为空", trigger: "blur" }
        ],
        documentType: [
          { required: true, message: "文件类型不能为空", trigger: "blur" }
        ],
        publishDate: [
          { required: true, message: "文件发布日期不能为空", trigger: "blur" }
        ],
        effectiveDate: [
          { required: true, message: "文件生效日期不能为空", trigger: "blur" }
        ],
      },
      orgList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报送文档列表 */
    getList() {
      this.loading = true;
      listDocument(this.queryParams).then(response => {
        this.documentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        documentId: null,
        orgId: null,
        documentName: null,
        documentType: null,
        receiverInfo: null,
        publishDate: null,
        effectiveDate: null,
        expiryDate: null,
        fileStorageId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.documentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      listOrg().then(response => {
        this.orgList = response.rows;
      });
      this.open = true;
      this.title = "添加报送文档";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      if (row.documentId != null) {
        listOrg().then(response => {
          this.orgList = response.rows;
        });
      }
      const documentId = row.documentId || this.ids
      getDocument(documentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报送文档";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.documentId != null) {
            updateDocument(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDocument(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const documentIds = row.documentId || this.ids;
      this.$modal.confirm('是否确认删除报送文档编号为"' + documentIds + '"的数据项？').then(function() {
        return delDocument(documentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('submit/document/export', {
        ...this.queryParams
      }, `document_${new Date().getTime()}.xlsx`)
    },
    /** 查询机构列表 */
    getOrgList() {
      listOrg().then(response => {
        this.orgList = response.rows;
      });
    }
  }
};
</script>
